import React, { PureComponent } from 'react'
import { connect } from 'dva';
import Link from 'umi/link';
import { Table, Popconfirm, Divider } from 'antd';

import Form from './components/SearchForm';
import Upload from './components/Upload'

export interface Props {
  dispatch?: Function,
  children?: React.ReactNode,
  loading: boolean,
  buttons?: Array<any>,
  list: {
    data: Array<any>,
    dataCnt: number
  };
  cond?: {
    pageSize: number,
  },
  show?: boolean;
  temple?: any;
}

@connect(({ fire_facilities, loading, menu }) => ({
  buttons: menu.buttons,
  cond: fire_facilities.cond || {},
  list: fire_facilities.list || {},
  temple: fire_facilities.temple,
  show: fire_facilities.show,
  loading: loading.effects['fire_facilities/queryPage']
}))
class Device extends PureComponent<Props, any> {
  constructor(props) {
    super(props);
    this.initCurrent = this.initCurrent.bind(this);
  }
  state = {
    current: 1
  };

  queryData(params) {
    const { dispatch } = this.props;

    dispatch({
      type: 'fire_facilities/queryPage',
      payload: params,
    });
  }

  initCurrent() {
    this.setState({
      current: 1
    })
  }

  onShowSizeChange = (current, pageSize) => {
    const { dispatch, cond } = this.props;
    cond.pageSize = pageSize;
    this.setState({ current: 1 }, () => {
      dispatch({
        type: 'fire_facilities/updateCond',
        payload: cond
      })
    })
  }

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({ type: `fire_facilities/initCond` });
  }

  remove = (id: string) => {
    const { dispatch } = this.props;
    this.setState({
      current: 1
    }, () => {
      dispatch({
        type: 'fire_facilities/remove',
        payload: id
      })
    })
  }

  render() {
    const { buttons } = this.props;
    const { data = [], dataCnt } = this.props.list;

    const btnList = {};
    buttons.forEach(item => {
      btnList[item.name] = item.name
    });

    const ans = data && Array.isArray(data) ? data.map((item, index) => ({
      key: index,
      ind: index + 1,
      id: item.id,
      addressName: `${item.buildName || ''}${item.floorName || ''}${item.areaName || ''}${item.installationAddr || ''}`,
      ...item,
    })) : [];

    const pagination = {
      total: dataCnt,
      showTotal: total => `共 ${total} 条`,
      showSizeChanger: true,
      current: this.state.current,
      pageSizeOptions: ['10', '20', '50', '100'],
      onShowSizeChange: this.onShowSizeChange,
      onChange: (current) => {
        this.setState({ current }, () => {
          this.queryData({ pageNum: current })
        });
      },
    };

    const columns = [{
      title: '序号',
      dataIndex: 'ind',
      render: (text) => <>{text < 10 ? `0${text}` : text}</>
    }, {
      title: '设施器材名称',
      dataIndex: 'name'
    }, {
      title: '所在位置',
      dataIndex: 'addressName'
    }, {
      title: '类型',
      dataIndex: 'deviceTypeName'
    }, {
      title: '厂家型号',
      dataIndex: 'product'
    }, {
      title: '操作', key: 'x',
      render: (record, text: number | string, index: number) => {
        return (
          <div style={{ display: 'flex' }}>
            <>
              <Link to={`/data/facilities/${record.id}/update`}>编辑</Link>
              <Divider type="vertical" />
            </>
            <Popconfirm
              title="请确认是否删除?"
              onConfirm={() => this.remove(record.id)} okText="确定" cancelText="取消"
            >
              <a>删除</a>
            </Popconfirm>
          </div>
        )
      }
    }];

    return (
      <>
        <Form current={this.state.current} initCurrent={this.initCurrent} />
        <Table
          columns={columns}
          pagination={pagination}
          dataSource={ans}
          bordered
          loading={this.props.loading}
        />
        <Upload show={this.props.show} temple={this.props.temple}/>
      </>
    );
  }
}

export default Device;
